/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cc.altius.web.controller;

import cc.altius.model.City;
import cc.altius.model.State;
import cc.altius.service.CityService;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;

/**
 *
 * @author santosh
 */
@Controller
public class CityController {

    @Autowired
    private CityService cityService;

    @ModelAttribute("stateList")
    public List<State> populateStateList() {
        List<State> stateList = this.cityService.getStateList();
        return stateList;
    }

//    @ModelAttribute("cityList")
//    public List<City> populateCityList() {
//        return this.cityService.getCityList();
//    }
    @RequestMapping(value = "/city/addCity.htm", method = RequestMethod.GET)
    public String ShowAddCityPage(ModelMap modelMap) {

        City city = new City();
        modelMap.addAttribute("city", city);
        return "city/addCity";
    }

    @RequestMapping(value = "/city/addCity.htm", method = RequestMethod.POST)
    public String onSubmit(@ModelAttribute("city") City city, Errors errors, HttpServletRequest request,
            SessionStatus sessionStatus) {
        if (this.cityService.existCityByCityName(city.getCityName())) {
            errors.rejectValue("cityName", "msg.duplicateCityName");
            //errors.rejectValue("zoneCode", "msg.duplicateZoneCode");
            return "/city/addCity";
        }
        Boolean flag = this.cityService.addCity(city);

        if (flag == true) {
            //return "redirect:../city/addCity.htm?msg=An error ocurred while adding city.. ";
            errors.rejectValue("cityName", "msg.cityNameError");
            return "/city/addCity";
        } else {
            sessionStatus.setComplete();
            return "redirect:../city/listCity.htm";
        }
    }

    @RequestMapping(value = "/city/listCity.htm", method = RequestMethod.GET)
    public String showlistCity(ModelMap modelMap) {
        List<Map<String, Object>> filterCity = this.cityService.getFilterCity(0);
        modelMap.addAttribute("filterCity", filterCity);
        modelMap.addAttribute("stateId", 0);
        return "/city/listCity";
    }

    @RequestMapping(value = "/city/listCity.htm", method = RequestMethod.POST)
    public String filterOnCity(@RequestParam(value = "stateId", required = true, defaultValue = "0") int stateId,
            ModelMap modelMap) {

        List<Map<String, Object>> filterCity = this.cityService.getFilterCity(stateId);
        modelMap.addAttribute("filterCity", filterCity);
        modelMap.addAttribute("stateId", stateId);

        return "city/listCity";
    }

    @RequestMapping(value = "/city/editCity.htm", method = RequestMethod.GET)
    public String showEditCity(@RequestParam("cityId") String cityId, ModelMap modelMap) {

        City city = this.cityService.getCityById(Integer.parseInt(cityId));
        modelMap.addAttribute("city", city);
        return "city/editCity";
    }

    @RequestMapping(value = "/city/editCity.htm", method = RequestMethod.POST)
    public String updateCity(@ModelAttribute("city") City city) {

        int cityId = this.cityService.editCity(city);
        if (cityId != 0) {
            return "redirect:../city/listCity.htm?msg=update success...";
        } else {
            return "redirect:../city/editCity.htm?msg=update failed...";
        }
    }
}
